﻿<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Bezdelnick
 * Date: 12.04.13
 * Time: 15:14
 * To change this template use File | Settings | File Templates.
 */
require_once("Config.php");
require_once("common/Lang.php");
require_once("persistence/Genre.php");
require_once("dao/GenreDao.php");

$genreDao = new GenreDao();
$genres = $genreDao->load();

if (isset($_POST['action'])) {
    if ($_POST['action'] == "add") {
        if (isset($_POST['add_genre_name'])) {
            $createdGenre = new Genre();
            $createdGenre->setName($_POST['add_genre_name']);
            $genreDao->insert($createdGenre);
        }
    } else if ($_POST['action'] == "edit") {
        if (isset($_POST['edit_genre_id']) && isset($_POST['edit_genre_name'])) {
            $editedGenre = new Genre();
            $editedGenre->setId($_POST['edit_genre_id']);
            $editedGenre->setName($_POST['edit_genre_name']);
            $genreDao->update($editedGenre);
        }
    } else if ($_POST['action'] == "remove") {
        if (isset($_POST['remove_genre_id'])) {
            $genreDao->delete($_POST['remove_genre_id']);
        }
    }
}

?>

<!-- Content table -->
<table class="content-table" border="0">
    <thead style="background-color: #ffd700;">
    <tr>
        <td style="width: 10%;">ID</td>
        <td style="width: 65%;">Name</td>
        <td style="width: 25%;">Operations</td>
    </tr>
    </thead>
    <tbody>
    <? foreach ($genres as $genre) { ?>
    <tr style="background-color: #efefef;">
        <td><?= $genre->getId() ?></td>
        <td><?= $genre->getName() ?></td>
        <td style="text-align: center;">
            <div class="jquery-button edit-genre-link"
                 itemId="<?= $genre->getId() ?>"><?= Lang::EDIT ?>
            </div>
            <div class="jquery-button remove-genre-link"
                 itemId="<?= $genre->getId() ?>"><?= Lang::REMOVE ?>
            </div>
        </td>
    </tr>
        <? } ?>
    </tbody>
</table>